package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * rabbit mq监听器
 *
 * @Author mr.gao
 * @Date 2025/1/5 10:57
 */
@Component
public class SpringRabbitListener {
    /**
     * 简单模型-消费者
     * @param msg
     */
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg) {
        System.out.println("spring rabbit mq 监听到的消息是：" + msg);
    }
    /**
     * work模型-消费者
     * @param msg
     */
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String msg) {
        System.out.println("消费者1-监听到的消息是：" + msg);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.out.println("消费者2-监听到的消息是：" + msg);
        TimeUnit.MILLISECONDS.sleep(100);
    }

    /**
     * 广播模型-消费者1
     */

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) {
        System.out.println("消费者1-监听到的消息是：" + msg);
    }
    /**
     *  广播模型-消费者2
     */
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) {
        System.out.println("消费者2-监听到的消息是：" + msg);
    }

    /**
     *  路由模型-消费者1
     */
   @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct .queue1"),
           exchange = @Exchange(name = "direct.exchange",type = ExchangeTypes.DIRECT),
           key = {"red","blue"}))
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者1-监听到的消息是：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct .queue2"),
            exchange = @Exchange(name = "direct.exchange",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}))
    public void listenDirectQueue2(String msg) {
        System.out.println("消费者2-监听到的消息是：" + msg);
    }

    /**
     *  topic 模型-消费者1
     */

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic .queue1"),
            exchange = @Exchange(name = "topic.exchange",type = ExchangeTypes.TOPIC),
            key = "*.red.*"))
    public void listenTopicQueue1(String msg) {
        System.out.println("消费者1-监听到的消息是：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic .queue2"),
            exchange = @Exchange(name = "topic.exchange",type = ExchangeTypes.TOPIC),
            key = "blue.#"))
    public void listenTopicQueue2(String msg) {
        System.out.println("消费者2-监听到的消息是：" + msg);
    }




























}


